
\subsection{Introduction}

At its core, \langname is based in {\em continuations}, rather than in
standard bytecode.  This allows us a powerful framework on which we
can build useful abstractions like coroutines and exception handling.
Even functions are implemented in a continuation-passing style, which
gives us a way to make the {\em tail recursion optimization}. With
continuations, recursive function calls at the end of functions avoid
any stack-style memory usage.

What is a continuation?  A continuation is an abstraction that
receives a value, does some computation, and yields another value. So
far, we've just described the basic abstraction of a {\em function}.
The key difference is that continuations {\em specify a next
  continuation where program flow will be transferred} instead of just
returning to wherever invoked them.  This ``next continuation''
pointer is referred to as the ``destination''.  Note that this allows
us to treat a chain of continuations as a continuation itself.
Finally, continuations are allowed to transfer control to {\em any}
other continuation, provided they suppy that continuation with their
current destination pointer.

Let's do an example: Say {\tt x} is 3 and {\tt y} is 2.  {\tt x + y}
is made up of the building-block continuations {\bf lookup}, {\bf
  accumulate}, and {\bf plus}.  {\bf lookup} takes its parameter
(ignoring the input), finds its value in the current environment, and
yields that value. {\bf plus} takes in a tuple of numerical values,
and yields their sum.  {\bf accumulate} is actually a continuation
that takes in another continuation (call it {\bf c}) as an argument
when it is created. {\bf accumulate} creates a new {\bf
  accumulate-helper} continuation parameterized with its input,
directs this new continuation it the destination of the {\bf
  accumulate}, directs {\bf c} at the {\bf accumulate-helper}, and
transfers control to {\bf c}.  {\bf accumulate-helper} takes its
input, puts it in a tuple with its parameter, and then yields that
tuple.  The overall continuation looks like this to start with:

{\bf lookup}(x)->{\bf accumulate}({\bf lookup}(y))->{\bf plus}->out

After the {\bf accumulate} runs, we'll have something like:

{\bf lookup}(y)->{\bf accumulate-helper}(3)->{\bf plus}->out

Which will execute through to yield the answer of 5, as it should.

So far, we've glossed over one important factor in this execution
model: how do we know what environment is current?  To accomplish
this, we allow for some storage space in the {\em thread of control}
itself.  We store the current environment in the thread of control,
and create special continuations to switch environments when needed.

\subsection{First-order continuations}

Of course, in \langname, continuations are objects, just like
everything else.  In fact, we provide a builtin function to access the
current continuation: {\tt call_cc}, which is short for ``call with
current continuation,'' the name of the equivalent function in
Scheme. {\tt call_cc} takes in a function, and calls it with the
current continuation (the one that {\tt call_cc} returns to) as an
argument.  That function can call the continuation, store it, or
whatever else.  For example, a synchronous version of a thread fork
could be implemented like so:

\begin{verbatim}

def thread_fork as {
  call_cc( as (cc){ cc(false) })
}

\end{verbatim}

Think about it.  It's a bit of a mind-bender, but this code returns
once from {\tt thread_fork} with {\tt false}, finishes up whatever it
was doing, and then returns {\em again} from the same place with a
value of whatever the first time yielded. Scary, huh?

Needless to say, {\tt call-cc} is a bit more low-level of an
abstraction than most people tend to use frequently, but other
abstractions like our version of coroutines and exception handling
take advantage of continuations under the hood.